package com.file.service.Impl;




import com.file.mapper.UserMapper;
import com.file.pojo.User;
import com.file.service.UserService;
import com.file.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public String login(User user) {
        String password = user.getPassword();
        //md5hash相对于md5更加"安全" 没有绝对安全的系统!!!
        String md5Pass =
                DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Pass);
        //根据其中不为null的数据当做where条件 u/p
        //Sql: select * from user where u=#{u} and p=#{p}
        if(user == null){
            //没有查询到数据
            return null;
        }
        String token = UUID.randomUUID().toString()
                .replace("-", "");
        return token;
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<User> selectUser(int pageNum,int pageSize,String query) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum",(pageNum-1)*pageSize);
        params.put("pageSize",pageSize);
        PageResult<User> pageResult = new PageResult<>();
//        List<User> list = userMapper.seleUserPage(params);
        List<User> listUser = userMapper.selectUser(query);//条件查询
//        pageResult.setQuery(listUser);
        pageResult.setRows(listUser);//返回数据
        pageResult.setTotal((int) userMapper.count());
        return pageResult;
    }

    //用户状态修改
    @Override
    public void updateStatus(User user) {
        userMapper.updateStatus(user);
    }

    //用户信息修改
    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    //删除用户
    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

}
